<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="身份证号" prop="id">
      <el-input v-model="dataForm.id" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="iphone">
      <el-input v-model="dataForm.iphone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="所属车商" prop="cardealer">
      <el-input v-model="dataForm.cardealer" placeholder="所属车商"></el-input>
    </el-form-item>
    <el-form-item label="驻派厂区" prop="purchasePlant">
      <el-input v-model="dataForm.purchasePlant" placeholder="驻派厂区"></el-input>
    </el-form-item>
    <el-form-item label="有效期" prop="datetime1">
      <el-input v-model="dataForm.datetime1" placeholder="有效期"></el-input>
    </el-form-item>
    <el-form-item label="照片目录以及文件名" prop="picture">
      <el-input v-model="dataForm.picture" placeholder="照片目录以及文件名"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          id: '',
          name: '',
          sex: '',
          iphone: '',
          cardealer: '',
          datetime1: '',
          picture: '',
          purchasePlant: ''
        },
        dataRule: {
          id: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          iphone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          purchasePlant: [
            { required: true, message: '驻派厂区不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/residentpersonnel/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.id = data.residentPersonnel.id
                this.dataForm.name = data.residentPersonnel.name
                this.dataForm.sex = data.residentPersonnel.sex
                this.dataForm.iphone = data.residentPersonnel.iphone
                this.dataForm.cardealer = data.residentPersonnel.cardealer
                this.dataForm.datetime1 = data.residentPersonnel.datetime1
                this.dataForm.picture = data.residentPersonnel.picture
                this.dataForm.purchasePlant = data.residentPersonnel.purchasePlant
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/residentpersonnel/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'id': this.dataForm.id,
                'name': this.dataForm.name,
                'sex': this.dataForm.sex,
                'iphone': this.dataForm.iphone,
                'cardealer': this.dataForm.cardealer,
                'datetime1': this.dataForm.datetime1,
                'picture': this.dataForm.picture,
                'purchasePlant': this.dataForm.purchasePlant
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
